﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DunLibrary.Base;
using DunLibrary.CreditCard;
using Telerik.Windows.Controls;

namespace DunSilverlight.Views
{
    public partial class ContactForm : UserControl
    {
        private CardholderContact contact;

        public Action<CardholderContact> SaveCallBack;

        public ContactForm(CardholderContact contact)
        {
            this.contact = contact;
            InitializeComponent();
            Loaded += new RoutedEventHandler(ContactForm_Loaded);
        }

        void ContactForm_Loaded(object sender, RoutedEventArgs e)
        {
            if (contact.ContactType != null)
            {
                cmbTypes.IsEnabled = false;
            }

            DataContext = contact;
            LoadContactTypes();//读取联系方式类别
        }

        private void LoadContactTypes()
        {
            Action<IList<ContactType>> callback = (IList<ContactType> types) =>
                {
                    cmbTypes.ItemsSource = types;
                };
            AppGlobal.GetContactTypes(callback);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (CheckVerify())
            {
                CardholderContactService.CardholderContactServiceClient ser = new CardholderContactService.CardholderContactServiceClient();

                ser.SaveOrUpdateCompleted += (object sender1, CardholderContactService.SaveOrUpdateCompletedEventArgs e1) =>
                    {
                        int id = Comm.JsonSerializerHelper.JsonToEntity<int>(e1.Result);
                        if (id>0)
                        {
                            contact.Id = id;//获得新增后的id
                            Comm.Tools.ShowMessage("保存成功!", "", true);
                            SaveCallBack(contact);
                        }
                    };

                contact.CreateTime = DateTime.Now;
                contact.Creator = AppGlobal.CurrentUser;

                string json = Newtonsoft.Json.JsonConvert.SerializeObject(contact);

                ser.SaveOrUpdateAsync(json);
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        private bool CheckVerify()
        {
            if (contact.ContactType == null)
            {
                Comm.Tools.ShowMessage("请选择类别!", "", false);
                return false;
            }

            if (string.IsNullOrEmpty(contact.Description))
            {
                Comm.Tools.ShowMessage("请输入描述", "比如：手机、家庭电话、单位电话等", false);
                return false;
            }

            if (string.IsNullOrEmpty(contact.Relation))
            {
                Comm.Tools.ShowMessage("请输入关系", "比如：本人、朋友、亲属等", false);
                return false;
            }

            if (string.IsNullOrEmpty(contact.Content))
            {
                Comm.Tools.ShowMessage("请输入内容", "比如：电话号码", false);
                return false;
            }

            return true;
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }
    }
}
